/**
 * Copyright (C) 2009 alex <me@alex.0x08.org>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package uber.netbsd.community.action;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontValidate;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.HandlesEvent;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.SimpleMessage;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.validation.Validate;
import uber.netbsd.community.dao.Person;
import uber.netbsd.community.dao.PersonManager;

/**
 * Stripes action to change users passwords
 * @author alex
 */
@UrlBinding("/action/ChangePassword")
public class ChangePasswordActionBean extends BaseActionBean {

    @Validate(required = true, minlength = 5, maxlength = 20, expression = "this == user.password")
    private String confirmPassword;
    private Person user;

    public Person getUser() {
        return user;
    }

    /**
     * Preloads Person object and forward to changePassword page
     * @return changePassword page
     */
    @DontValidate
    public Resolution preEdit() {

        this.user = PersonManager.getInstance().getPerson(user.getId());

        return new ForwardResolution("/admin/changePassword.jsp");
    }

    public void setUser(Person u) {
        this.user = u;
    }

    /**
     * User pressed button "Save"
     * @return redirect url
     */
    @HandlesEvent("Save")
    @DefaultHandler
    public Resolution saveChanges() {

        if (user.isNew()) {
            throw new SecurityException("Try to change password for non-exist user. THIS IS A BUG.");
        }


        Person ruser = PersonManager.getInstance().getPerson(user.getId());
        ruser.setPassword(user.getPassword());

        PersonManager.getInstance().saveOrUpdate(ruser);
        getContext().getMessages().add(new SimpleMessage("Пароль успешно сохранен. Такие дела."));
        return new RedirectResolution("/action/Profile").addParameter("preEdit", "").addParameter("user.id", ruser.getId());
    }

    /**
     * User cancelled password change
     * @return profile url
     */
    @HandlesEvent("Cancel")
    @DontValidate
    public Resolution cancelChanges() {
        return new RedirectResolution("/profile/"+user.getId());
    }


    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

    
    public String getConfirmPassword() {
        return confirmPassword;
    }
}
